﻿package org.display.util
{
	import flash.filesystem.File;
	import flash.utils.*;
	
	import mx.controls.Alert;
	
	import org.abcFrame.core.AbcEvent;
	import org.abcFrame.core.AbcEventDispatcher;
	import org.data.*;
	import org.events.AbcNetEvent;
	import org.net.*;
	import org.utils.*;
	
	import mx.managers.CursorManager;

	public class UploadAll
	{
		var upfiles:Array=[]
			
		var acceptMessage:Array=[]
			
		var http:String="http://www.wakao123.com/test/up.php"
			
		var httpxml:String="http://www.wakao123.com/test/upxml.php"
			
		var filelist:String="http://www.wakao123.com/test/filelist.xml"
			
		var paths:Array=["swf/org/display/component","xml/org/display/component",
			"org/commands/component","org/abcFrame","org/data","org/enmu","org/net","org/utils",
			"jsfl/org/display/component" ]
			
		public function UploadAll()
		{
			AbcEventDispatcher.instance.addEventListener(AbcNetEvent.UploadFile,UploadFileEvent)
				
			var file:File=File.documentsDirectory.resolvePath((Global.workspace).replace(/\\/gis,"/"))
			
			reExcept(file)
			
			if(upfiles.length>0){
				
				new Upload(upfiles.shift(),http)
				
			}else{
				
				Alert.show("今天没有更新任何文件！")
				
			}
			
		}
		
		function reExcept(file:File){
		
			var files:Array=file.getDirectoryListing() 
			
			for(var i=0;i<files.length;i++){
				
				var file:File=files[i] as File
				
				if(files[i].isDirectory){
					
					reExcept(files[i])
					
				}else if(Global.username=="aishi"){
				
				
					for(var j=0;j<paths.length;j++){
						
						var check:String = paths[j].replace(/\//gis,"\\")
						
						if(file.nativePath.indexOf(check)!=-1){
							
							upfiles.push(file.nativePath)
							
						}
						
					}
				
				
				
				}else if(file.nativePath.indexOf(Global.username)!=-1){

					//trace(file.nativePath)
					
					var date:Date=new Date()
					var today:int=new Date(date.fullYear,date.month,date.date).valueOf()
					
					var value:int=Math.max(file.creationDate.valueOf(),file.modificationDate.valueOf())
					
					if(value>=today){
					 
					  upfiles.push(file.nativePath)
						  
					}
					
				}
				
			}

		}
		
		
		function UploadFileEvent(e:AbcEvent){
			
			//trace(e.data)
			
			var arr:Array=e.data.split(":")
				
			acceptMessage.push({time:arr[0],fileurl:arr[1]})
		
			if(upfiles.length>0){
				
				new Upload(upfiles.shift(),http)
				
			}else{

				AbcEventDispatcher.instance.removeEventListener(AbcNetEvent.UploadFile,UploadFileEvent)

				var res:AbcResponder=new AbcResponder()
					
				res.onResult=function(e,path){
					
					var data:XML=XML(e)
					
					for(var i=0;i<acceptMessage.length;i++){
						data.appendChild(<item>{acceptMessage[i].time+"/"+acceptMessage[i].fileurl}</item>)
					}
					
					//trace(data.toXMLString())
	
					FileControl.saveFile(Global.workspace+"/filelist.xml",data.toXMLString())
					
					setTimeout(function(){new Upload(Global.workspace+"/filelist.xml",httpxml)},100)

					AbcEventDispatcher.instance.addEventListener(AbcNetEvent.UploadFile,completeEvent)
					
					}
					
				//改写服务器配置文件
				new AbcHTTPServices(filelist,res)
					
			}
			
			CursorManager.setBusyCursor()

		}
		
		
		function completeEvent(e:AbcEvent){
			
			CursorManager.removeAllCursors()
		
			//trace(e.data)
			Alert.show("上传完毕！")
				
			AbcEventDispatcher.instance.removeEventListener(AbcNetEvent.UploadFile,completeEvent)
		
		
		}
		
		
		
	}
}